<?php
class acl
{
	function __construct()
	{
		$this->CI =& get_instance();    
	}
	
	function check($pm_check='',$module='',$function='',$url_redirect='')
	{
        $login_name = $this->CI->session->userdata('LoginName');
        if($login_name=="supperadministrator")
            return true;
        $access=false;
        if($module=='')
            $module = $this->CI->router->fetch_module();
        if($function=='')
            $function = $this->CI->router->fetch_class();
        
        $group_id = $this->CI->session->userdata('group_id');
        
        
        $this->CI->db->select('permission');
        
        $this->CI->db->join('admin_module',"admin_right.module_id=admin_module.module_id");
        $this->CI->db->join('admin_function',"admin_right.function_id=admin_function.function_id");
        
		$this->CI->db->where('admin_right.group_id',$group_id);
		$this->CI->db->where('admin_module.module_code',$module);
        $this->CI->db->where('admin_function.function_code',$function);
        //print_r($this->CI->session->all_userdata());
		$permission = $this->CI->db->get('admin_right')->row(); 
        //echo $group_id.$module.$function;
        //print_r($permission);
		if($permission)
		{        
			if($permission->permission!=''){
                $arr_permission = json_decode($permission->permission,true);
                if(isset($arr_permission[$pm_check])&&$arr_permission[$pm_check]==1)
                {
                    $this->write_logs($module,$function);
                    $access=true;
                }
            }
		}
        if($url_redirect!=''&&$access==false){
            redirect($url_redirect);
        }else
            return $access;
	}
	
	function write_logs($Modules,$Function)
	{
		$Url = $this->CI->session->userdata('Url');
		$admin_id = $this->CI->session->userdata('admin_id');
		$data = array(
			'admin_id' => $admin_id,
			'modules' => $Modules,
			'function' => $Function,
			'url' => $Url,
			'log_info' =>$this->CI->agent->agent_string(),
			'log_ip' => $this->CI->input->ip_address(),
			'date_log' => time()
		);
		$this->CI->db->insert('logs',$data);
	}
}
?>
